<?php 
$page_title = "21 Game - Register";
include("header.php"); ?>

<!-- page header -->
<div class="page-header">
	<div class="container">
		<h1><span style="color: red;">Register new Account</span></h1>
	</div>
</div>
<!-- end page header -->
<?php 
if($_SERVER['REQUEST_METHOD'] == "POST"){

	require_once("sql/connection.php"); // Connect to the db
	require_once("utilities/functions.php");

	$errors = array();	//Initialize errors array

	//Check nickname
	if(empty($_POST['nickname'])){
		$errors[] = 'You forgot to enter your nickname.';
	}
	elseif(checkNickname($dbc,$_POST['nickname'])){
		$nm = mysqli_real_escape_string($dbc,trim($_POST['nickname']));
	}
	else{
		$errors[] = "This nickname is not available.";
	}

	//Check email
	if(empty($_POST['email'])){
		$errors[] = 'You forgot to enter your email address.';
	}
	else{
		if(validEmail($_POST['email'])){
			if(checkEmail($dbc,$_POST['email'])){
				$e = mysqli_real_escape_string($dbc,trim($_POST['email']));
			}
			else{
				$errors[] = "This email address is already in use!.";
			}
		}
		else{
			$errors[] = "This email address in not valid.";
		}
	}

	//Check password
	if(empty($_POST['pass']) AND empty($_POST['pass2'])){
		$errors[] = "You forgot to enter your password";
	}
	elseif($_POST['pass'] != $_POST['pass2']){
		$errors[] = "The password does not match.";
	}
	else{
		$pw = mysqli_real_escape_string($dbc,trim($_POST['pass']));
	}

	//If no errors occurred insert the new user in the database
	if(empty($errors)){
		if(registerUser($dbc,$nm,$e,$pw)){
			
			echo "<p class='lead'>You have successfully created your account. You are now available to play against other players.";
			header( "Refresh:5; url=index.php" );

			sendEmail($e,$nm,$pw);
		}
		else{
			echo "<p class='text-danger'>Some error occurred while you were trying to create an account. Please try later. Sorry for the inconvenience.";
		}
	}
	else{
		echo "<p class='lead'>The following error(s) occurred</p>";
		foreach ($errors as $data) {
			echo "<p class='text-danger'>".$data."</p>";
		}
		echo "<a href='register.php'>Back</a>";
	}
	echo "</div>";
}
else{
	?>
<!--content-->
<div class="container">
	<div class="col-lg-3">
		<form action="" method="post" role="form">
			<div class="form-group">
				<label for="nickname">Nickname</label>
				<input type="text" class="form-control" name="nickname" maxlength="20" placeholder="Nickname">
			</div>
			<div class="form-group">
				<label for="name">Email</label>
				<input type="text" class="form-control" name="email" maxlength="100" placeholder="example@domain.com">
			</div>
			<div class="form-group">
				<label for="password">Password</label>
				<input type="password" class="form-control" name="pass" maxlength="40" placeholder="Insert password"><br>
				<input type="password" class="form-control" name="pass2" maxlength="40" placeholder="Confirm password">
			</div>
			<input type="submit" class="btn btn-primary btn-lg btn-block" style="background:red" value="Register">
		</form>
	</div>
</div>
<!--end content-->
	<?php
}
 ?>

<?php include("footer.php"); ?>